
#include "EquationSolver.hpp"

const double pi=acos(-1);

// implement the funciton of derived class function1.
// The action is based on EquationSolver.h to solve the roots of special equation.
// example:
//    (Temporarily omitted.)
class Function1:public Function{
public:
    double operator()(const double &x) const {
        return (1.0/x-tan(x));
    }
    void sol1()
    {
        Bisection solver1((*this),0.0,pi/2,_eps,1000);
        std::cout<<"(I) f(x)=1/x-tanx=0. ";
        solver1.solve();
        std::cout<<std::endl;
    }
}f1;

// implement the funciton of derived class function2.
// The action is based on EquationSolver.h to solve the roots of special equation.
// example:
//    (Temporarily omitted.)
class Function2:public Function{
public:
    double operator()(const double &x) const {
        return (1.0/x-pow(2.0,x));
    }
    void sol2()
    {
        Bisection solver2((*this),0.0,1.0,_eps,1000);
        std::cout<<"(II) f(x)=1/x-2^x=0. ";
        solver2.solve();
        std::cout<<std::endl;
    }
}f2;

// implement the funciton of derived class function3.
// The action is based on EquationSolver.h to solve the roots of special equation.
// example:
//    (Temporarily omitted.)
class Function3:public Function{
public:
    double operator()(const double &x) const {
        return (pow(2.0,-x)+exp(x)+2*cos(x)-6);
    }
    void sol3()
    {
        Bisection solver3((*this),1.0,3.0,_eps,1000);
        std::cout<<"(III) f(x)=2^(-x)+e^x+2cosx-6=0. ";
        solver3.solve();
        std::cout<<std::endl;
    }
}f3;

// implement the funciton of derived class function4.
// The action is based on EquationSolver.h to solve the roots of special equation.
// example:
//    (Temporarily omitted.)
class Function4:public Function{
public:
    double operator()(const double &x) const {
        return ((pow(x,3.0)+4*pow(x,2.0)+3*x+5)/(2*pow(x,3.0)-9*pow(x,2.0)+18*x-2));
    }
    void sol4()
    {
        Bisection solver4((*this),0.0,4.0,_eps,1000);
        std::cout<<"(IV) f(x)=(x^3+4x^2+3x+5)/(2x^3-9x^2+18x-2)=0. ";
        solver4.solve();
        std::cout<<std::endl;
    }
}f4;


int main()
{
    std::cout<<"The Assignment B tests Bisection method by several equations as follows."<<std::endl<<std::endl;
    f1.sol1();
    f2.sol2();
    f3.sol3();
    f4.sol4();
    std::cout<<"--------------------Test succeeds.--------------------"<<std::endl<<std::endl;
    return 0;
}

